home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 April / EnigmA AMIGA RUN 17 (1997)(G.R. Edizioni)(IT)[!][issue 1997-04][EAR-CD].iso / EARCD / comm / amiex / KLR_ChatSRC.lha / Chat-O-Meter_Top.C < prev    next >
C/C++ Source or Header  |  1995-06-29  |  5KB  |  220 lines

  1. #define COM_TOP
  2.  
  3. #include "Chat-O-Meter.H"
  4.  
  5. //#define CHATTOPDEBUG
  6.  
  7. extern struct CHATTOP         *chat_ptr;
  8. extern struct TODAY_DATA    *td_ptr;
  9. extern struct Library        *DOSBase;
  10. extern UBYTE  ErrStr[];
  11.  
  12. void TopTen( ULONG t )
  13. {
  14.     /* This routine will update the Chat-O-Top.Data file if it exists
  15.      * and if it does not exist it will first create a new, empty one
  16.      * before updating it.
  17.      */
  18.  
  19.     char    temp[ 100 ];
  20.  
  21.     UBYTE    DataFile[] = "PROGDIR:Chat-O-Top.Data";
  22.  
  23.     ULONG    i, j ;
  24.  
  25.     USHORT slotnumber;
  26.  
  27.     FILE    *fptr;
  28.  
  29.     BPTR    fh;
  30.     
  31.     // Initialize soms vars
  32.     getuserstring( temp , DT_SLOTNUMBER );
  33.     slotnumber = atoi( temp );
  34.     chat_ptr->Time = 0;
  35.     chat_ptr->Chats = 0;
  36.  
  37.     if ( access( DataFile , F_OK ) )
  38.     {
  39.         // Does not exist
  40.         if ( fh = Open( DataFile , MODE_NEWFILE ) )
  41.         {
  42.             for ( j = 1 ; j <= slotnumber ; j ++ )            
  43.                 Write( fh , chat_ptr , sizeof( struct CHATTOP ) );
  44.             Close( fh );
  45.         }
  46.         else
  47.         {
  48.             sprintf( temp , ErrStr , "create Chat-O-Top data file." );
  49.             sm( temp ,1 );
  50.             enddoor( FALSE );
  51.         }
  52.     }
  53.     else
  54.     {
  55.         if ( fptr = fopen( DataFile , "r+" ) )
  56.         {
  57.             fseek( fptr , 0 , SEEK_END );
  58.             i = ftell( fptr );    // FileSize
  59.             if ( ( i / sizeof( struct CHATTOP ) ) < slotnumber )
  60.             {
  61.                 for ( j = 1 ; j <= ( slotnumber - ( i / sizeof( struct CHATTOP ) ) ) ; j ++ )
  62.                     fwrite( chat_ptr , sizeof( struct CHATTOP ) , 1 , fptr );
  63.             }
  64.             fclose( fptr );
  65.         }
  66.         else
  67.         {
  68.             sprintf( temp , ErrStr , "access Chat-O-Top data file." );
  69.             sm( temp , 1 );
  70.             enddoor( FALSE );
  71.         }
  72.     }
  73.     
  74.     // Okay, by now it should be no problem to write the new data in it
  75.     if ( fh = Open( DataFile , MODE_OLDFILE ) )
  76.     {
  77.         Seek( fh , ( slotnumber - 1 ) * sizeof( struct CHATTOP ) , OFFSET_BEGINNING );
  78.         Read( fh , chat_ptr , sizeof( struct CHATTOP ) );
  79.  
  80.         /* We need to check if this is a new user, we don't want new users
  81.          * profiting from the old users' time (if he was deleted).
  82.          */
  83.         chat_ptr->Time += t;
  84.         chat_ptr->Chats++;
  85.         Seek( fh , ( slotnumber - 1 ) * sizeof( struct CHATTOP ) , OFFSET_BEGINNING );
  86.         Write( fh , chat_ptr , sizeof( struct CHATTOP ) );
  87.         Close( fh );
  88.     }
  89.     else
  90.     {
  91.         sprintf( temp , ErrStr , "write Chat-O-Top data file." );
  92.         sm( temp , 1 );
  93.         enddoor( FALSE );
  94.     }
  95. }
  96.  
  97. BOOL Today ( ULONG time_chatted )
  98. {
  99.     /* This routine will update the .today file of Chat-O-Meter, this
  100.      * means that this chat's time will be added to the time of any
  101.      * previous chats of today. If a new day has begun, the result
  102.      * will be compared to the previous record. If a new record has
  103.      * been set, this routine will return TRUE else FALSE.
  104.      */
  105.  
  106.     UBYTE        DayFile[] = "PROGDIR:Chat-O-Meter.Today";
  107.  
  108.     char        temp[ 200 ];
  109.  
  110.     ULONG        hours,
  111.                 mins;
  112.  
  113.     time_t    t;
  114.  
  115.     BPTR        fh;
  116.  
  117.     BOOL        new_record = FALSE;    /* RETURN VALUE */
  118.  
  119.     struct    ClockData    now;
  120.  
  121.     struct    Library        *UtilityBase;
  122.  
  123.     if ( UtilityBase = OpenLibrary( "utility.library" , 37 ) )
  124.     {
  125.         /* Get current date/time */
  126.         time( &t );
  127.         t -= 252482400;    /* UNIX TIME OFFSET */
  128.         Amiga2Date( t , &now );
  129.  
  130.         /* Convert time */
  131.         hours = time_chatted / 3600;
  132.         time_chatted -= hours * 3600;
  133.         mins = time_chatted / 60;
  134.         time_chatted -= mins * 60;
  135.  
  136.         /* Check for today file */
  137.         if ( access ( DayFile , F_OK ) )
  138.         {
  139.             /* NOT FOUND */
  140.             if ( fh = Open( DayFile , MODE_NEWFILE ) )
  141.             {
  142.                 td_ptr -> tt_Time  = hours * ( 60 * 60 );                
  143.                 td_ptr -> tt_Time += mins  * 60;
  144.                 td_ptr -> tt_Time += time_chatted;
  145.  
  146.                 td_ptr -> tt_Day   = now.mday;
  147.                 td_ptr -> tt_Month = now.month;
  148.                 td_ptr -> tt_Year  = now.year;
  149.  
  150.                 td_ptr -> tt_RecYear = 1900;
  151.  
  152.                 Write( fh , td_ptr , sizeof( struct TODAY_DATA ) );
  153.                 Close( fh );
  154.             }
  155.         }
  156.     
  157.         /* Open the .today file and check it */
  158.         if ( fh = Open( DayFile , MODE_OLDFILE ) )
  159.         {
  160.             /* Pick up old data */
  161.             Read( fh , td_ptr , sizeof( struct TODAY_DATA ) );
  162.  
  163.             /* Is it still today? */
  164.             if ( now.mday == td_ptr -> tt_Day ) 
  165.             {
  166.                 td_ptr -> tt_Time += hours * ( 60 * 60 );
  167.                 td_ptr -> tt_Time += mins  * 60;
  168.                 td_ptr -> tt_Time += time_chatted;
  169.                 td_ptr -> tt_Chats++;
  170.  
  171.                 Seek ( fh , 0 , OFFSET_BEGINNING );
  172.                 Write( fh , td_ptr , sizeof( struct TODAY_DATA ) );
  173.             }
  174.             else
  175.             {
  176.                 /* It's a new day, hurray hurray ;) */
  177.                 if ( td_ptr -> tt_RecTime < td_ptr -> tt_Time &&
  178.                       td_ptr -> tt_Chats > 0 )
  179.                 {
  180.                     /* A new record, yeeehaaa, copy record data */
  181.                     td_ptr -> tt_RecTime  = td_ptr -> tt_Time;
  182.                     td_ptr -> tt_RecChats = td_ptr -> tt_Chats;
  183.                     td_ptr -> tt_RecDay   = td_ptr -> tt_Day;
  184.                     td_ptr -> tt_RecMonth = td_ptr -> tt_Month;
  185.                     td_ptr -> tt_RecYear  = td_ptr -> tt_Year;
  186.                     new_record = TRUE;
  187.                 }
  188.  
  189.                 /* Record or not, we need to reset the data for today */
  190.                 td_ptr -> tt_Time  = hours * ( 60 * 60 );                
  191.                 td_ptr -> tt_Time += mins  * 60;
  192.                 td_ptr -> tt_Time += time_chatted;
  193.  
  194.                 td_ptr -> tt_Day   = now.mday;
  195.                 td_ptr -> tt_Month = now.month;
  196.                 td_ptr -> tt_Year  = now.year;
  197.  
  198.                 /* Save the updated data file */
  199.                 Seek ( fh , 0 , OFFSET_BEGINNING );
  200.                 Write( fh , td_ptr , sizeof( struct TODAY_DATA ) );
  201.             }
  202.             Close( fh );
  203.         }
  204.         else
  205.         {
  206.             sprintf( temp , ErrStr , "read Chat-O-Meter.Today file." );
  207.             sm( temp , 1 );
  208.             enddoor( FALSE );
  209.         }
  210.         CloseLibrary( UtilityBase );
  211.     }
  212.     else
  213.     {
  214.         sprintf( temp , ErrStr , "open utility.library" );
  215.         sm( temp , 1 );
  216.         enddoor( FALSE );
  217.     }
  218.     return( new_record );
  219. }
  220.